from DrissionPage import ChromiumPage

class AutoMation:
    def __init__(self,page:ChromiumPage)-> None:
        self.page = page
        
    def start(self,url,username,password):       
        # 跳转到登录页面
        self.page.get(url)
        # 定位到账号文本框，获取文本框元素，输入对文本框输入账号
        self.page.ele('@name=username').input(username)
        # 定位到密码文本框并输入密码
        self.page.ele('@name=password').input(password)
        # 验证码
        yzm = input('enter code:')
        self.page.ele('@name=vercode').input(yzm)
        # 点击登录按钮
        self.page.ele('@value=登录').click()

    def article_manage(self):
        self.page.wait(0.5)
        self.page.ele('内容管理').click()
        self.page.wait(0.5)
        self.page.ele('内容列表').click()

    def article_copy(self,webseo):
        # 选择要复制的文章并复制
        self.page.ele('@data-index=0').ele('.layui-icon layui-icon-ok').click()
        self.page.ele('批量复制').click()
        self.page.wait.ele_displayed('.layui-layer-btn0')
        self.page.ele('.layui-layer-btn0').click()
        #编辑复制的文章
        self.page.wait(2)
        self.page.ele('@@lay-event=edit@@text()=编辑').click()
        # 清空需要填充的输入框
        self.page.ele('@id=title').clear()
        self.page.ele('@id=keywords').clear()
        self.page.ele('@id=seo_title').clear()
        self.page.ele('@name=litpic').clear()
        # 标题
        self.page.ele('@id=title').input(webseo['title'])
        # seo标题
        self.page.ele('@id=seo_title').input(webseo['title'])
        # 关键词
        self.page.ele('@id=keywords').input(webseo['keywords'])
        # 封面
        self.page.ele('@name=litpic').input(webseo['litpic'])
        # 保存
        self.page.ele('tag:button@@lay-filter=save').click()
        # title重复
        while self.page.ele('title重复！',timeout=0.2):
            self.page.ele('@@class=layui-layer-btn0@@text()=确定').click()
            self.page.ele('@id=title').input('，4008053357')
            self.page.ele('@id=title').input(',{}'.format(webseo['title']))
            self.page.ele('@id=seo_title').input('，4008053357')
            self.page.ele('@id=seo_title').input(',{}'.format(webseo['title']))
            self.page.ele('tag:button@@lay-filter=save').click()
        #返回列表
        self.page.ele('tag:a@@text()=返回列表').click()
        self.page.wait(0.5)
